package com.mdnote.jianzhi_offer.greedy;

/**
 * @author Rhythm-2019
 * @version 1.0
 * @date 2020/10/18 0018
 * @description
 */
public class Offer14II {

    public int cuttingRope(int n) {

        if (n < 2) {
            return 0;
        }

        if (n == 2) {
            return 1;
        }

        if (n == 3) {
            return 2;
        }
        // 先少乘1个
        int a = n / 3 - 1, b = n % 3, c = 1000000007;
        int mod = loopMod(3, a, c);

        System.out.println(mod);
        System.out.println(fastMod(3,n / 3 - 1, c));
        if (b == 0){
            return (int) ((mod * 3) % c);
        } else if (b == 1) {
            return (mod * 4) % c;
        } else {
            return (mod * 2 * 3) % c;
        }
    }

    private long fastMod(long x, long a, long p) {
        long res = 1;
        while (a > 0) {
            if ((a & 1) == 1) {

                res = (res * x) % p;
            }
            x = (x * x) % p;
            a /= 2;
        }
        return res;
    }

    public int loopMod(int x, int a, int p) {
        int res = 1;
        for (int i = 0; i < a; i++) {
            res = (res * x ) % p;
        }
        return res;
    }

    public static void main(String[] args) {
        Offer14II offer14II = new Offer14II();
        int i = offer14II.cuttingRope(120);
        System.out.println(i);
    }


}
